if exists(select name from sysobjects where name = 'sp_KhuyenMaiHotGetAll')
	drop proc sp_KhuyenMaiHotGetAll
go
/*-- =============================================
	Created by: HP
	Created on: 2014-06-03 (dd/mm/yyyy)
	Description: 
	--

	File:$sql-scripts/Proc.sp_KhuyenMaiHotGetAll.sql
*/-- =============================================

CREATE PROCEDURE sp_KhuyenMaiHotGetAll
    @PageIndex INT = 1,
	@PageSize INT = 10,
	@RecordCount INT=0 OUTPUT,
	@ThanhPho int
AS
BEGIN
	select ROW_NUMBER() over(
		order by (100-CAST(GiaKM AS float)/CAST(GiaNiemYet AS float)*100) desc
	) as RowNumber ,
		a.[KhuyenMaiId],
		a.[TieuDe],
		a.[GiaNiemYet],
		a.[GiaKM],
		a.[ThoiGian],
		a.[Link],
		a.[ImageSmall],
		a.[SiteNguon],
		a.[Provider],
		a.[Summary],
		a.[ThuTu],
		a.[TrangThai],
		a.[NguoiTao],
		a.[NgayTao],
		a.[NguoiSua],
		a.[NgaySua],
		a.[WebId]
    into #Results
	FROM
        KhuyenMai a WHERE TrangThai<>255 and a.thanhpho=@thanhpho
    set @RecordCount=(select COUNT(KhuyenMaiId) from #Results)
	select * from #Results  WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1 order by  (100-CAST(GiaKM AS float)/CAST(GiaNiemYet AS float)*100) desc
	drop table #Results
END
GO


